<template>
  <view>
    <u-navbar :title="hospitalNameFormat" title-width="400" :is-back="false" :background="{ backgroundColor:'#9CE0FF'}" :border-bottom="false" title-color="#000" title-size="37">
      <template v-slot:right>
        <image class="w-48 h-48 mr-10" src="@/static/images/home/locate.png"  @tap="handleHospitalTap"></image>
      </template>
    </u-navbar>
    <!-- <u-navbar v-else title="净化后勤助手" :is-back="false" :background="{ backgroundColor:'#9CE0FF'}" :border-bottom="false" title-color="#000" title-size="37" /> -->
    <view class="flex align-center justify-between bg-9CE0FF p-lr-20">
      <view class="flex align-center">
        <image class="w-100 h-100" src="@/static/images/common/yk-logo2.png"></image>
        <text class="fz-40 ml-40 sys-title">净化后勤助手</text>
      </view>
      <image class="w-215 h-184" src="@/static/images/home/note.png"></image>
    </view>
    <view class="mt-20">
      <sub-title title="本日概览"></sub-title>
      <view class="p-17">
        <view class="fz-30 color-888888">个人待完成</view>
        <view class="flex align-center justify-between mt-11">
          <view class="p-16 box-shadow flex align-center flex1 br-10 mr-10" @tap="toUndone('work')">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/inspection.png"></image>
            <view>
              <view class="fz-18 color-888888">巡检</view>
              <view class="fz-48 color-333333">{{ personTotal.inspectionCount || 0 }}</view>
            </view>
          </view>
          <view class="p-16 box-shadow flex align-center flex1 br-10 mr-10" @tap="toUndone('maintenance')">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/maintenance.png"></image>
            <view>
              <view class="fz-18 color-888888">保养</view>
              <view class="fz-48 color-333333">{{ personTotal.maintenanceCount || 0 }}</view>
            </view>
          </view>
          <view class="p-16 box-shadow flex align-center flex1 br-10" @tap="toUndone('repair')">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/repair.png"></image>
            <view>
              <view class="fz-18 color-888888">维修</view>
              <view class="fz-48 color-333333">{{ personTotal.repairCount || 0 }}</view>
            </view>
          </view>
        </view>
        <view v-if="isManagerOrAdmin">
        <view class="fz-30 color-888888 mt-20">团队事项</view>
        <view class="flex align-center justify-between mt-11">
          <view class="p-16 box-shadow flex align-center flex1 br-10 mr-10" @tap="toAssign('work')">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/inspectionUnassign.png"></image>
            <view>
              <view class="fz-18 color-888888">巡检待分配</view>
              <view class="fz-48 color-333333">{{ toBeAssignedTotal.inspectionCount || 0 }}</view>
            </view>
          </view>
          <view class="p-16 box-shadow flex align-center flex1 br-10 mr-10" @tap="toAssign('maintenance')">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/maintenanceUnassign.png"></image>
            <view>
              <view class="fz-18 color-888888">保养待分配</view>
              <view class="fz-48 color-333333">{{ toBeAssignedTotal.maintenanceCount || 0 }}</view>
            </view>
          </view>
          <view class="p-16 box-shadow flex align-center flex1 br-10" @tap="toAssign('repair')">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/repairUnassign.png"></image>
            <view>
              <view class="fz-18 color-888888">报修待处理</view>
              <view class="fz-48 color-333333">{{ toBeAssignedTotal.repairCount || 0 }}</view>
            </view>
          </view>
        </view>
        <view class="flex align-center justify-between mt-11">
          <view class="p-16 box-shadow flex align-center flex1 br-10 mr-10" @tap="toInspectionUndone">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/inspection.png"></image>
            <view>
              <view class="fz-18 color-888888">巡检待完成</view>
              <view class="fz-48 color-333333">{{ pendingTotal.inspectionCount || 0 }}</view>
            </view>
          </view>
          <view class="p-16 box-shadow flex align-center flex1 br-10 mr-10" @tap="toMaintenanceUndone">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/maintenance.png"></image>
            <view>
              <view class="fz-18 color-888888">保养待完成</view>
              <view class="fz-48 color-333333">{{ pendingTotal.maintenanceCount || 0 }}</view>
            </view>
          </view>
          <view class="p-16 box-shadow flex align-center flex1 br-10" @tap="toRepairUndone">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/repair.png"></image>
            <view>
              <view class="fz-18 color-888888">维修待完成</view>
              <view class="fz-48 color-333333">{{ pendingTotal.repairCount || 0 }}</view>
            </view>
          </view>
        </view>
        <view class="flex align-center justify-between mt-11">
          <view class="p-16 box-shadow flex align-center flex1 br-10 mr-10" @tap="toInspectionDone">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/inspectionDone.png"></image>
            <view>
              <view class="fz-18 color-888888">巡检已完成</view>
              <view class="fz-48 color-333333">{{ finishTotal.inspectionCount || 0 }}</view>
            </view>
          </view>
          <view class="p-16 box-shadow flex align-center flex1 br-10 mr-10" @tap="toMaintenanceDone">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/maintenanceDone.png"></image>
            <view>
              <view class="fz-18 color-888888">保养已完成</view>
              <view class="fz-48 color-333333">{{ finishTotal.maintenanceCount || 0 }}</view>
            </view>
          </view>
          <view class="p-16 box-shadow flex align-center flex1 br-10" @tap="toRepairDone">
            <image class="w-64 h-64 mr-10" src="@/static/images/home/repairDone.png"></image>
            <view>
              <view class="fz-18 color-888888">维修已完成</view>
              <view class="fz-48 color-333333">{{ finishTotal.repairCount || 0 }}</view>
            </view>
          </view>
        </view>
      </view>
      </view>
    </view>
    <view class="mt-14">
      <sub-title title="操作工具"></sub-title>
      <view class="flex align-center justify-between mt-30 p-lr-46 pb-30 color-888888">
          <view class="flex flex-column align-center " @tap="createFixOrder">
            <image class="w-100 h-100" src="@/static/images/home/start-repair.png"></image>
            <view class="fz-28 mt-8">发起报修</view>
          </view>
          <view class="flex flex-column align-center relative" @tap="toAbnormalList">
            <image class="w-100 h-100" src="@/static/images/home/fix-abnormal.png"></image>
            <view class="fz-28 mt-8">异常修正</view>
            <text class="absolute top-0 h-34 bg-FF6262 br-17 p-lr-10 color-fff fz-24 lh-34" style="right: -14rpx" v-if="showData(errorInfoCount)">{{ showData(errorInfoCount)}}</text>
          </view>
          <view class="flex flex-column align-center" @tap="scanCode">
            <image class="w-100 h-100" src="@/static/images/home/pair-input.png"></image>
            <view class="fz-28 mt-8">纸质录入</view>
          </view>
          <view class="flex flex-column align-center relative" @tap="toMessageList">
            <image class="w-100 h-100" src="@/static/images/home/msg.png"></image>
            <view class="fz-28 mt-8">消息</view>
            <text class="absolute top-0 h-34 bg-FF6262 br-17 p-lr-10 color-fff fz-24 lh-34" style="right: -14rpx" v-if="showData(unReadMessage)">{{ showData(unReadMessage)}}</text>
          </view>
        </view>
      <!-- <view class="m-lr-20 m-tb-30">
       <view class="h-75 flex align-center justify-between p-lr-20 border-bottom-CCCCCC" @tap="toEquipment">
         <text>设备扫描结果临时入口</text>
         <image class="w-9 h-18" src="@/static/images/common/toPath.png"></image>
       </view>
       <view class="h-75 flex align-center justify-between p-lr-20 border-bottom-CCCCCC" @tap="toSign">
         <text>护士签名临时入口</text>
         <image class="w-9 h-18" src="@/static/images/common/toPath.png"></image>
       </view>
      </view> -->

      <sub-title title="数据统计"></sub-title>
      <view class="flex align-center m-lr-46 justify-between pb-30 mt-30">
        <view class="text-center flex flex-column" @tap.stop="staffStatistics">
          <image class="w-100 h-100" src="@/static/images/statistics/staff.png"></image>
          <text class="fz-28 color-888888 pt-8">人员统计</text>
        </view>
        <view class="text-center flex flex-column" v-if="isManagerOrAdmin" @tap.stop="deviceStatistics">
          <image class="w-100 h-100" src="@/static/images/statistics/device.png"></image>
          <text class="fz-28 color-888888 pt-8">设备统计</text>
        </view>
        <view class="text-center flex flex-column" v-if="isManagerOrAdmin" @tap.stop="hospitalStatistics">
          <image class="w-100 h-100" src="@/static/images/statistics/hospital.png"></image>
          <text class="fz-28 color-888888 pt-8">医院统计</text>
        </view>
        <view class="w-100 h-100">
        </view>
      </view>
    </view>
    <u-modal border-radius="20" :show-confirm-button="false" :show-title="false" mask-close-able v-model="isModalShow">
      <view class="h-150 flex align-center justify-center font-bold">
        <image class="mr-20 w-38 h-38" src="@/static/images/mine/error.png"></image>
        <text class="fz-30 color-333333 err-text">{{modalContent}}</text>
      </view>
    </u-modal>
    <CustomUTabbar></CustomUTabbar>
  </view>
</template>

<script>
  import CustomUTabbar from '../components/CustomUTabbar'
  import SubTitle from "@/components/SubTitle/SubTitle";
  import {
    todoCount,
    assignedStats,
    todoStats,
    finishStats,
  } from "@/api/basic/home"
  import {abnormalCount} from "@/api/inspection/abnormal"
  import {recordHistory} from "@/api/inspection/record"
  import {unreadCount} from "@/api/basic/message";
  import {getHospital} from '@/api/hospital/hospital'
  import {mapGetters} from 'vuex'

  export default {
    components: {SubTitle, CustomUTabbar},
    data() {
      return {
        pendingTotal: {},
        finishTotal: {},
        toBeAssignedTotal: {},
        personTotal:{},
        errorInfoCount: 0,
        unReadMessage: 0,
        isManagerOrAdmin:false,
        isEngineer: false,
        isModalShow:false,
        modalContent: '',
        hospitalInfo: {
          name: '全部',
          hospitalId: ''
        },
      }
    },
    computed: {
      ...mapGetters(['curHospitalId']),
      hospitalNameFormat(){
        const hospitalName = this.hospitalInfo.name
        const length = hospitalName.length
        if(length>8){
          return hospitalName.slice(0,5) + '...' + hospitalName.slice(length-4)
        }else{
          return hospitalName
        }
      }
    },
    onLoad(){
      this.isManagerOrAdmin = this.$auth.hasRoleOr(['manager','admin','co-admin'])
      this.isEngineer = this.$auth.hasRole('engineer')
    },
    onShow(){
      this.initData()
    },
    methods:{
      //请求数据
      initData() {
        this.initHospitalInfo()
        //未读消息数
        unreadCount().then(res => {
          this.unReadMessage = res.data || 0
        })
        //异常工单数
        abnormalCount().then(res => {
          this.errorInfoCount = res.data || 0
        })
        if(this.isManagerOrAdmin) {
          //个人
          todoCount().then(res => {
            this.personTotal = res.data
          })
          //团队待分配
          assignedStats().then(res => {
            this.toBeAssignedTotal = res.data
          })
          //团队待完成
          todoStats().then(res => {
            this.pendingTotal = res.data
          })
          //团队已完成
          finishStats().then(res => {
            this.finishTotal = res.data
          })
        }else {
          todoCount().then(res => {
            this.personTotal = res.data
          })
        }
      },
      initHospitalInfo() {
        if (!this.curHospitalId) {
          this.hospitalInfo = {name: '全部', hospitalId: ''}
          return Promise.resolve('hospitalId为空')
        }
        getHospital(this.curHospitalId).then(res => {
          this.hospitalInfo = res.data || {name: '医院信息异常', hospitalId: ''}
          return Promise.resolve(res)
        })
      },
      handleHospitalTap() {
        uni.navigateTo({url: '/pages/mine/replace-hospital'})
      },
      staffStatistics(){
        this.$tab.navigateTo('/pages/statistics/staff')
      },
      deviceStatistics(){
        this.$tab.navigateTo('/pages/statistics/device')
      },
      hospitalStatistics(){
        this.$tab.navigateTo('/pages/statistics/hospital')
      },
      showData(data) {
        return data > 99 ? "99+" : data;
      },
      //发起报修
      createFixOrder(){
        uni.navigateTo({url: '/pages/repair/start-repair-work-order'})
      },
      scanCode(){
        uni.scanCode({
          scanType: ['qrCode','datamatrix'],
          success: (res) => {
            console.log('条码内容：' + res.result);
            // planId=xxxxx&startDate=yyyy-MM-dd&endDate=yyyy-MM-dd
            // res.result = `planId=1600044669751361538&startDate=2022-12-01&endDate=2022-12-31`
            let data = res.result.split('&').reduce((pre, next) => {
              pre[next.split('=')[0]] = next.split('=')[1]
              return pre
            },{})

            if(!data.planId ||!data.startDate || !data.endDate ){
              this.modalContent = '非纸质版工单二维码，请重新扫码!'
              this.isModalShow = true
            }
            //入参非空
            else{
              recordHistory(data).then(res => {
                if(res.code !== 200){
                  this.modalContent = '工单二维码扫描异常'
                  this.isModalShow = true
                }
                //响应体的code为200
                else{
                  if(!res.data.length){
                    this.modalContent = '无巡检工单！'
                    this.isModalShow = true
                  }
                  else if(res.data.length === 1){
                    this.$tab.navigateTo('/pages/work/work-order-detail?workOrderId=' + res.data[0].workOrderId)
                  }
                  else{
                    this.$tab.navigateTo('/pages/home/batch-entry?data=' + JSON.stringify(data))
                  }
                }
              })
            }
          },
        });
      },
      toBatchEntry(){
        this.$tab.navigateTo('/pages/home/batch-entry')
      },
      toAbnormalList(){
        this.$tab.navigateTo('/pages/home/abnormal-list')
      },
      toMessageList(){
        this.$tab.navigateTo('/pages/home/message-list')
      },
      toEquipment() {
        this.$tab.navigateTo('/pages/equipment/equipment')
      },
      toSign() {
        this.$tab.navigateTo('/pages/sign/sign')
      },
      toInspectionDone(){
        this.$store.commit('SET_ORDERTYPE', 'done')
        this.toInspection()
      },
      toInspectionUndone(){
        this.$store.commit('SET_ORDERTYPE', 'unDone')
        this.toInspection()
      },
      toInspection(){
        this.$store.commit('SET_TABSCURRENT', 4)
        this.$store.commit('SET_TABSCURRENTNAME', '全部工单')
        this.$store.commit('SET_ISFROMHOME2WORK', true)
        this.$tab.switchTab('/pages/tabbar/work/work')
      },
      toMaintenanceDone(){
        this.$store.commit('SET_ORDERTYPE', 'done')
        this.toMaintenance()
      },
      toMaintenanceUndone(){
        this.$store.commit('SET_ORDERTYPE', 'unDone')
        this.toMaintenance()
      },
      toMaintenance(){
        this.$store.commit('SET_ISFROMHOME2WORK', true)
        this.$store.commit('SET_TABSCURRENT', 3)
        this.$store.commit('SET_TABSCURRENTNAME','全部工单')
        this.$tab.switchTab('/pages/tabbar/maintenance/maintenance')
      },
      toRepairDone(){
        this.$store.commit('SET_ORDERTYPE', 'done')
        this.toRepair()
      },
      toRepairUndone(){
        this.$store.commit('SET_ORDERTYPE', 'unDone')
        this.toRepair()
      },
      toRepair(){
        this.$store.commit('SET_ISFROMHOME2WORK', true)
        this.$store.commit('SET_TABSCURRENT', 3)
        this.$store.commit('SET_TABSCURRENTNAME', '全部工单')
        this.$tab.switchTab('/pages/tabbar/repair/repair')
      },
      toAssign(type){
        this.$store.commit('SET_ISFROMHOME2WORK', true)
        this.$store.commit('SET_TABSCURRENT', 0)
        this.$store.commit('SET_TABSCURRENTNAME', '待分配')
        this.$tab.switchTab(`/pages/tabbar/${type}/${type}`)
      },
      toUndone(type){
        this.$store.commit('SET_ISFROMHOME2WORK', true)
        this.$store.commit('SET_TABSCURRENT', this.isManagerOrAdmin ? 1 : 0)
        this.$store.commit('SET_TABSCURRENTNAME', '待完成')
        this.$tab.switchTab(`/pages/tabbar/${type}/${type}`)
      }
    }
  }
</script>
<style lang="scss">
page{
  background-color: #FFFFFF;
}
</style>
<style lang="scss" scoped>
.sys-title {
  font-family: Source Han Sans CN-Regular;
}
.box-shadow{
  box-shadow: 0rpx 3rpx 6rpx 1rpx rgba(0,0,0,0.16);
}
</style>
